//
//  CourseViewController.m
//  MM
//
//  Created by 王晨辉 on 15/5/18.
//  Copyright (c) 2015年 王晨辉. All rights reserved.
//

#import "CourseViewController.h"
#import "RegistrationViewController.h"
#import "DetailsCourseViewController.h"
@interface CourseViewController ()

@end

@implementation CourseViewController{
    
    BOOL ifRead;
}


-(void)viewWillAppear:(BOOL)animated{
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString *babyID = [user1 objectForKey:@"babyID"];
//
    NSDictionary *param4 = @{@"userId":ID};
    [UserDAL getAllClassesWithParams:param4 withBlock:^(NSArray *data, NSError *error) {
        if (data) {
            
            

            if (data.count!=0) {
            nameArr = [NSMutableArray array];
            numberArr = [NSMutableArray array];
            oldArr = [NSMutableArray array];
            idsArray = [NSMutableArray array];
            for (NSDictionary * dic in data) {
                
                NSString * nameStr = [dic objectForKey:@"className"];
                ////NSLog(@"what%@",nameStr);
                [nameArr addObject:nameStr];
                
                NSString * numberStr = [dic objectForKey:@"classNewPrice"];
                ////NSLog(@"what%@",numberStr);
                [numberArr addObject:numberStr];

                NSString * oldStr = [dic objectForKey:@"classOldPrice"];
                ////NSLog(@"what%@",oldStr);
                [oldArr addObject:oldStr];

                NSString * ids = [dic objectForKey:@"id"];
                ////NSLog(@"what%@",ids);
                [idsArray addObject:ids];
                
                
                
            }
            }else{
                
                alert(@"对不起，你所在的地区没有麦苗公学。");
                
            }
            
            
            [_tableView reloadData];
            
            
            
            
            
        }else
        {
            
        }
    }];
    
    
    //设置导航栏头文字
    self.navigationItem.title = @"预约课程";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#ef5858" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];    //左边按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight =  50;
    _tableView.userInteractionEnabled = YES;
    [self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    
    [self subView];
    
    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    
    
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}



-(void)subView{
    

    
    
    
}
-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)baby{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}

#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return nameArr.count;
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    
    
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _tableView.rowHeight-1, SCREEN_WIDTH, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    [cell addSubview:view];
    
    
    
    NSNumber * all = [numberArr valueForKeyPath:@"@sum.floatValue"];
    ////NSLog(@"%@",all);
    

    UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH/2, 50)];
    nameLab.text = [NSString stringWithFormat:@"《%@》",nameArr[indexPath.row]];
    nameLab.font = [UIFont systemFontOfSize:16];
    nameLab.textAlignment = NSTextAlignmentLeft;
    [cell   addSubview:nameLab];
        
    UILabel * timeLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 0, 60, 50)];
    timeLab.text = [NSString stringWithFormat:@"%@元",oldArr[indexPath.row]];
    timeLab.textColor = [UIColor blackColor];
    timeLab.font = [UIFont systemFontOfSize:16];
    timeLab.textAlignment = NSTextAlignmentLeft;
    [cell   addSubview:timeLab];
    
    UIView * timeView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-105, _tableView.rowHeight/2-1, 50, 1)];
    timeView.backgroundColor = [UIColor blackColor];
    [cell addSubview:timeView];
    
    
    
    
    UILabel * NewtimeLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50, 0, 50, 50)];
    NewtimeLab.text = [NSString stringWithFormat:@"%@元",numberArr[indexPath.row]];
    NewtimeLab.textColor = [UIColor orangeColor];
    NewtimeLab.font = [UIFont systemFontOfSize:16];
    NewtimeLab.textAlignment = NSTextAlignmentLeft;
    [cell   addSubview:NewtimeLab];
    
    
    
    
        _tableView.tableFooterView = [[UIView alloc]init];

    
 
    return cell;
    
    
    
    
    
}

#pragma mark--- but点击事件

-(void)chooseBoy{
    

    ////NSLog(@"12455");
    
    
//    RegistrationViewController * reg = [[RegistrationViewController alloc]init];
////    reg.number = 
//    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:reg];
//    [self  presentViewController:nav animated:YES completion:nil];

    

    
    
}


#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    

        
        DetailsCourseViewController * reg = [[DetailsCourseViewController alloc]init];\
        reg.number = [NSString stringWithFormat:@"%@元",numberArr[indexPath.row]];
        reg.ids = idsArray[indexPath.row];
        reg.name = nameArr[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:reg];
        [self  presentViewController:nav animated:YES completion:nil];

        


    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
